$("#tagSearchDiv").on('click', '.tagSpan', function () {
    var size = $(this).css('border-width');
    if(size === '0px') {
        $(this).css('border-width', '3px');
        $(this).attr('isSelect', 'true');
    } else {
        $(this).css('border-width', '0px');
        $(this).attr('isSelect', 'false');
    }
});


$("#catagoryDiv").on('click', '.oneCategoryText', function () {
    $(this).siblings('.oneCategoryText').css('border', '0px black solid')
    $(this).siblings('.oneCategoryText').attr('isSelect', 'false');

    $(this).css('border', '3px black solid');
    $(this).attr('isSelect', 'true');

    loadTwoCategory();
});

$("#catagoryDiv").on('dblclick', '.twoCategoryText', function () {
    var c_id = $(this).attr('c_id');
    if($('.selectCategoryText[c_id="'+c_id+'"]').length > 0) {
        return;
    }

    var clone = $(this).clone().removeClass('twoCategoryText').addClass('selectCategoryText');
    $('.selectCategoryDiv').append(clone);
});

$("#catagoryDiv").on('click', '.selectCategoryText', function () {
    var size = $(this).css('border-width');
    if(size === '0px') {
        $(this).css('border-width', '3px');
        $(this).attr('isSelect', 'true');
    } else {
        $(this).css('border-width', '0px');
        $(this).attr('isSelect', 'false');
    }
});

$("#catagoryDiv").on('click', '.contentCategorySpan', function () {
    var c_id = $(this).attr('c_id');
    var c_name = $(this).attr('c_name');

    if($('.selectCategoryText[c_id="'+c_id+'"]').length > 0) {
        return;
    }

    $('.selectCategoryDiv').append('<span class="selectCategoryText" c_id="'+c_id+'">'+c_name+'</span>');
});

function loadOneCategory() {
    $.ajax({
        url :  '/category/all/one',
        type : 'post',
        async : false,
        dataType: 'json',
        success : function(data) {
            if(!data.list) {
                return;
            }

            $('.oneCategoryDiv').html('');
            for (let i = 0; i < data.list.length; i++) {
                var item = data.list[i];
                $('.oneCategoryDiv').append(
                    ' <span class="oneCategoryText" c_id="'+item.c_id+'">' +
                    item.c_name +
                    '</span>');
            }

            $('.oneCategoryDiv .oneCategoryText').eq(0).click();
        }
    });
}
loadOneCategory();

function loadTwoCategory() {
    $.ajax({
        url :  '/category/all/two?pId='+( $('span[isSelect="true"]').attr('c_id')),
        type : 'post',
        async : false,
        dataType: 'json',
        success : function(data) {
            if(!data.list) {
                return;
            }

            $('.twoCategoryDiv').html('');
            for (let i = 0; i < data.list.length; i++) {
                var item = data.list[i];
                $('.twoCategoryDiv').append(
                    ' <span class="twoCategoryText" c_id="'+item.c_id+'">' +
                    item.c_name +
                    '</span>');
            }
        }
    });
}


function searchTag( ) {
    $("#tag_list").html('');
    var tag_list = $("#tag_list").table({
        url : '/tag/all?tName=' + $('#searchTagName').val(),
        dataType : 'json',
        colModel : [
            // $('input[type="checkbox"]').prop('checked', this.checked);
            {
                display : '<input type="checkbox" onclick="$(\'.inputTagClass\').prop(\'checked\',  this.checked)"/>',
                width : '1%',
                align : 'left',
                sortable : true,
                format : function(data) {
                    data.t_name += "("+data.contentCount+")";
                    return '<input class="inputTagClass" type="checkbox" value="'+data.t_id+'" name="'+data.t_name+'"/>';
                }
            },{
                display : '标签名',
                width : '20%',
                align : 'left',
                sortable : true,
                format : function(data) {
                    return data.t_name;
                }
            },{
                display : '标签说明',
                width : '20%',
                align : 'left',
                format : function(data) {
                    return data.t_desc;
                }
            }
        ],
        rp : 10,
        showpager : 3,
        usepager : true,
        sortname : 't_id',
        sortorder : 'desc'
    });
}
function tagConfirmSelect() {
    var $selectTagList = $(".inputTagClass:checked")
    for (let i = 0; i < $selectTagList.length; i++) {
        var item = $selectTagList.eq(i);

        //已存在就不添加
        if($('.tagSpan[tid="'+item.val()+'"]').length > 0) {
            continue;
        }

        $('#tagSearchDiv').append(
            $("#tag_span_template").render({
                't_id' : item.val(),
                't_name' : item.attr('name')
            })
        );
    }
}

function searchTagOpen( ) {
    var openLay = layer.open({
        type: 1,
        title : '搜索标签',
        area: ['500px', '700px'],
        content: '    <div id="tagSearchOpenDiv"> ' +
            '        <p style="padding: 20px 10px;"> ' +
            '            <input type="text" id="searchTagName" placeholder="输入搜索标签"/> ' +
            '            <button onclick="searchTag()">搜索</button> ' +
            '        </p> ' +
            '        <div id="tag_list"> ' +
            '        </div> ' +
            '    </div>',
        btn: ['确认'] ,
        yes: function(index, layero){
            tagConfirmSelect();
            layer.closeAll();
        }
    });

    searchTag();
}

//查询个数
$.ajax({
    url :  '/content/count',
    dataType: 'json',
    success : function(data) {
        $('#contentNum').text(data.contentNum);
        $('#categoryNum').text(data.categoryNum);
        $('#tagNum').text(data.tagNum);
    }
});


$("#contenList").on('click', '.contentTagSpan', function () {
    var t_id = $(this).attr('t_id');
    var t_name = $(this).attr('t_name');

    if($('.tagSpan[tid="'+t_id+'"]').length > 0) {
        return;
    }

    $('#tagSearchDiv').append( $("#tag_span_template").render({
        't_id' : t_id,
        't_name' : t_name
    }));
});

var cacheContent = {};
function searchContent( ) {
    var param = {
        'pageNo' : 1,
        'pageSize' : 10,
        'tIdsOperation' : $('#tIdsOperation').val()
    };
    if($('#cName').val() != ''){
        param['cName'] = $('#cName').val();
        param['cDesc'] = $('#cName').val();
    }

    var selectList = $('.selectCategoryText');
    var categoryList = [];
    for (let i = 0; i < selectList.length; i++) {
        categoryList[categoryList.length] = selectList.eq(i).attr('c_id');
    }

    var tagList = [];
    selectList = $('.tagSpan');
    if(selectList.length > 0) {
        for (let i = 0; i < selectList.length; i++) {
            tagList[tagList.length] = selectList.eq(i).attr('tid');
        }
    }

    if(categoryList.length > 0){
        param['categroyIds'] = categoryList;
    }

    if(tagList.length > 0){
        param['tIds'] = tagList;
    }

    $("#contenList").html('');
    cacheContent = {};
    $("#contenList").table({
        url : '/content/search',
        params : param,
        gridTemplate : function(data){
            //关键字打红
            var keyword = $('#cName').val();
            // data.c_name = data.c_name.replace(/ /g, '&nbsp;');
            if(keyword != '') {
                var reg = new RegExp(keyword, 'g');
                data.c_name = data.c_name.replace(reg, '<span style="color: red; font-weight: bold;">'+keyword+'</span>');
                data.c_desc = data.c_desc.replace(reg, '<span style="color: red; font-weight: bold;">'+keyword+'</span>');
            }

            cacheContent[data.c_id] = data;

            if(data.tagList){
                for (var idx in data.tagList) {
                    data.tagList[idx].t_name += "("+data.tagList[idx].contentCount+")";
                }
            }

            return  $('#context_template').render(data);
        },
        userView : true,
        view : 'grid',
        rp : 20,
        hideHeader : false,
        // sortname : 'c_',
        // sortorder : 'desc',
        usepager : true,
        onSuccess : function(data){
        }

    });
}


function previewContent(cId) {
    var data = cacheContent[cId];
    var text = data.c_name;
    let match = text.match ( /\*\*[\s\S]*?\*\*/g);
    if (match) {
        for (let i = 0; i < match.length; i++) {
            var newVal = match[i].substr(2);
            newVal = newVal.substr(0, newVal.length - 2);
            text = text.replace(match[i], "<span style='font-weight:bold;'>"+ newVal +"</span>");
        }
    }

    text = text.replace(/\n/g, "<br/>")
        .replace(/\t/g, "<span style='    display: inline-block; width: 2em;'></span>");

    layer.open({
        type: 1,
        title : '预览',
        area: ['80%', '80%'],
        content: '<div id="previewDiv" style="padding: 10px">' + text + '</div>',
    });
}



















